function $(id) {
  return document.getElementById(id);
}

window.onload = function () {
  showbook(bookArray);
}
//填充表格
/**
 * 显示书籍信息
 * @param bookArray 书籍数组
 */
function showbook(bookArray) {
  let str = "";
  bookArray.forEach(n => {
    str += `<tr onclick="choice(this,${n.code})">
            <td>${n.bookname}</td>
            <td>${n.auto}</td>
            <td>${n.price}</td>
        </tr>`
  });
  $("bookData").innerHTML = str;
}

let colorTr = null;

/**
 * 选中行变色
 * @param trObj 选中的行对象
 */
function choice(trObj,bookId){
  if(colorTr != null){
    colorTr.style.backgroundColor = "white";
  }
  trObj.style.backgroundColor = "yellow";
  colorTr = trObj;

  //得到id锁对应的对象
  let bookObj = bookArray.find(n => n.code == bookId);
  //更新详细信息
  $("idSpan").innerHTML = bookObj.code;
  $("bookNameSpan").innerHTML = bookObj.bookname;
  $("priceSpan").innerHTML = bookObj.price;
  $("autoSpan").innerHTML = bookObj.auto;
  $("bookImg").src = bookObj.imgpath;

}

//根据书名模糊查询
function searchBook(){
  //按图书名进行筛选
  var newArray = bookArray.filter(n => n.bookname.indexOf($("searchName").value) != -1);
  //刷新
  showbook(newArray);
}


